//
//  MYCollectionViewController.m
//  草
//
//  Created by caimengjun on 16/12/6.
//  Copyright © 2016年 caimengjun. All rights reserved.
//

#import "MYCollectionViewController.h"

#import "CollectionViewCell.h"

#import "MYFooterView.h"
#import "MYHeaderView.h"


#define kMargin 10  //定义宏  上下左右边缘距10

@interface MYCollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout> //UICollectionViewDelegateFlowLayout有关样式的设置

@end

@implementation MYCollectionViewController

static NSString * const reuseIdentifier = @"MYCell";

static NSString * const reuseIdentifierHeader = @"MYHeaderCell";
static NSString * const reuseIdentifierFooter = @"MYFooterCell";


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    // Register cell classes
    //[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    [self.collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:reuseIdentifier];
    
    //注册header，footer
    [self.collectionView registerNib:[UINib nibWithNibName:@"MYHeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:reuseIdentifierHeader];
    [self.collectionView registerNib:[UINib nibWithNibName:@"MYFooterView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:reuseIdentifierFooter];
    
    
    //设置多选  再次点击后为取消点击
    self.collectionView.allowsMultipleSelection = YES;
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 3;//2段
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 8;//9个items单元格
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    //设置样式／数据
    cell.photoImageView.image = [UIImage imageNamed:@"photo"];
    cell.label.text = [NSString stringWithFormat:@"%ld,%ld",(long)indexPath.section,(long)indexPath.row];//用来显示第几段第几行。
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

//选中item
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    //点击进入另一个视图
   /* UIViewController * vi = [UIViewController new];
    vi.view.backgroundColor = [UIColor brownColor];
    
    //[self.navigationController pushViewController:vi animated:YES];
    [self presentViewController:vi animated:YES completion:^{
        
    }];
    */
    
    
    //点击变颜色
    CollectionViewCell *cell = (CollectionViewCell *) [self.collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor greenColor];
}

//取消选中item
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    //获取点击的单元格
    CollectionViewCell *cell = (CollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor blueColor];//没有被选中后变成蓝色
}


#pragma mark -形状设置

//cell大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    //获取屏幕的大小
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    
    //计算cell的宽度
    CGFloat cellWidth = (screenWidth - 3*kMargin) * 0.5;
    
    return CGSizeMake(cellWidth,cellWidth);//根据屏幕来去cell的大小
}

//整体边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(kMargin, kMargin, kMargin, kMargin);
}

//cell之间的横向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return kMargin;
}

//cell行距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return kMargin;
}


#pragma mark -补充视图数据源方法
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *supplementaryView;
    
    
    //
    if([kind isEqualToString:UICollectionElementKindSectionHeader] ){
        //这里是设置header的属性
        MYHeaderView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:reuseIdentifierHeader forIndexPath:indexPath];
        view.headerLabel.text =[NSString stringWithFormat:@"header: %d",indexPath.section];
        supplementaryView = view;
        
    }else if ([kind isEqualToString:UICollectionElementKindSectionFooter]){
        //这里是设置footer的属性
        MYHeaderView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:reuseIdentifierHeader forIndexPath:indexPath];
        view.headerLabel.text =[NSString stringWithFormat:@"这里是footer: %d",indexPath.section];
        supplementaryView = view;

    }
    
    
    
    return supplementaryView;
}


//设置header，footer 的高度  的代理方法
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    //获取屏幕宽度的方法
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    
    return CGSizeMake(screenWidth, 64);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    return CGSizeMake(screenWidth, 60);
}


@end
